package com.zhu.algorithm.jzOffer.linkedList;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zhujiabao
 * @Date: 2023/01/27/20:36
 * @Description: JZ6 从尾到头打印链表
 */
public class Solution06 {

    public static void main(String[] args) {
//        构建链表
        ListNode headNode = new ListNode(0);
        ListNode listNode = headNode;
        for (int i = 1; i < 10; i++) {
            listNode.next = new ListNode(i);
            listNode = listNode.next;
        }

        Solution06 solution06 = new Solution06();
//        solution06.printListFromTailToHead(headNode);
        System.out.println(solution06.printListFromTailToHead(headNode).toString());
    }

    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        int length = 0;

        ListNode tempNode = listNode.next;
        while (null != tempNode) {
            length++;
            tempNode = tempNode.next;
        }

        ArrayList<Integer> arrayList = new ArrayList<>(length);
        Integer[] array = new Integer[length];
        tempNode = listNode.next;
        while (null != tempNode){
            array[--length] = tempNode.val;
            tempNode = tempNode.next;
        }
        Collections.addAll(arrayList, array);

        return arrayList;
    }


}


